home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / flex_247.zip / flex_247 / MISC / debflex.awk < prev    next >
Text File  |  1993-09-18  |  2KB  |  92 lines

  1. # Clarify the flex debug trace by substituting first line of each rule.
  2. # Francois Pinard <pinard@iro.umontreal.ca>, July 1990.
  3. #
  4. # Sample usage:
  5. #    flex -d PROGRAM.l
  6. #    gcc -o PROGRAM PROGRAM.c -lfl
  7. #    PROGRAM 2>&1 | gawk -f debflex.awk PROGRAM.l
  8. #
  9. # (VP's note: this script presently does not work with either "old" or
  10. #  "new" awk [I haven't tried mawk]; fixes so it does will be welcome)
  11.  
  12. BEGIN {
  13.     # Insure proper usage.
  14.  
  15.     if (ARGC != 2) {
  16.         print "usage: gawk -f debflex.awk FLEX_SOURCE <DEBUG_OUTPUT"
  17.         exit (1)
  18.     }
  19.  
  20.     # Remove and save the name of flex source.
  21.  
  22.     source = ARGV[1]
  23.     ARGC--
  24.  
  25.     # Swallow the flex source file.
  26.  
  27.     line = 0
  28.     section = 1
  29.     while (getline <source) {
  30.  
  31.         # Count the lines.
  32.  
  33.         line++
  34.  
  35.         # Count the sections.  When encountering section 3,
  36.         # break out of the awk BEGIN block.
  37.  
  38.         if (match ($0, /^%%/)) {
  39.             section++
  40.             if (section == 3) {
  41.                 break
  42.             }
  43.         }
  44.  
  45.         # Only the lines in section 2 which do not begin in a
  46.         # tab or space might be referred to by the flex debug
  47.         # trace.  Save only those lines.
  48.  
  49.         if (section == 2 && match ($0, /[^ \t]/)) {
  50.             rules[line] = $0
  51.         }
  52.     }
  53. }
  54.  
  55. # Simplify trace of buffer reloads.
  56.  
  57. /^--\(end of buffer or a NUL\)/ {
  58.     print "-----------------------------------------------------------"
  59.     next
  60. }
  61.  
  62. # Remove trace of newlines.  This is debatable, but adequate for the
  63. # precise application this was developped for.
  64.  
  65. /^--accepting rule at line [0-9]+ \("$/ {
  66.     next
  67. }
  68.  
  69. /^"\)$/ {
  70.     next
  71. }
  72.  
  73. # Modify other trace lines to ease GNU emacs next-error processing,
  74. # also insert the related first line of flex source.
  75.  
  76. /^--accepting rule at line [0-9]+ \(".*"\)$/ {
  77.     if (rules[$5]) {
  78.         string = substr ($0, index ($0, "(") + 1)
  79.         string = substr (string, 1, length (string) - 1)
  80.         printf "%s(%d): %-8s -- %s\n", source, $5, string, rules[$5]
  81.     }
  82.     else {
  83.         print
  84.         printf "%s(%d): *** No such rule.\n", source, $5
  85.     }
  86.     next
  87. }
  88.  
  89. # Copy everything else verbatim.
  90.  
  91.     { print }
  92.